package cn.itcast.com.relection.method;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class demo1 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class targetClass = MethodClass.class;
        //获取多个方法
        Method[] methods = targetClass.getMethods();
        System.out.println(Arrays.toString(methods));
        System.out.println("****************");
        //Method[] getDeclaredMethods() 当前类中，所定义的所有方法
        Method[] declaredMethods = targetClass.getDeclaredMethods();
        System.out.println(Arrays.toString(declaredMethods));
        System.out.println("****************");
        //调用
        MethodClass targetObj = new MethodClass();
        Method privateMethod = targetClass.getDeclaredMethod("privateMethod", int.class, String.class);
        //绕过权限检查机制
        privateMethod.setAccessible(true);
        privateMethod.invoke(targetObj,10,"zhangsan");


    }

}

class MethodClass{
    //public访问
    public boolean publicMethod(int a, double b){
        System.out.println("publicMethod");
        return true;
    }
    //protected访问
    protected double protectedMethod(String name){
        System.out.println("protectedMethod");
        return 5.5;
    }
    //default访问
    void defultMethod(boolean canDo){
        System.out.println("defultMethod");
    }
    //private访问
    private int privateMethod(int inValue, String name){
        System.out.println("privateMethod");
        return 10;
    }

}
